package com.eastedu.template.core.render;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.eastedu.exception.ServiceException;
import com.eastedu.report.common.util.FileUtil;
import com.eastedu.template.model.ReportDataContainer;
import com.eastedu.template.util.RenderConfigUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * The type Default report render.
 *
 * @author superman
 */
@Slf4j
public class DefaultReportRender implements ReportRender {

    @Override
    public String render(ReportDataContainer container, InputStream template) throws IOException {
        // some code
        return this.render(container, template, (Configure) null);
    }

    @Override
    public void render(ReportDataContainer container, InputStream template, OutputStream outputStream) throws IOException {
        // some code
        this.render(container, template, outputStream, null);
    }

    @Override
    public String render(ReportDataContainer container, InputStream template, Configure configure) throws IOException {
        String file = FileUtil.generateTempFile(container.getTaskId(), container.getFileName());
        this.render(container, template, Files.newOutputStream(Paths.get(file)), configure);
        return file;
    }

    @Override
    public void render(ReportDataContainer container, InputStream template, OutputStream outputStream, Configure configure) throws IOException {
        if (Objects.isNull(outputStream)) {
            throw new ServiceException("outputStream is null");
        }
        // some code
        log.debug("[{}]开始生成报告: {}", container.getTaskId(), container.getParameter().getSchoolName());
        XWPFTemplate compile = RenderConfigUtil.createTemplate(template, configure);
        compile.render(container.getData());
        compile.writeAndClose(outputStream);
    }

}
